<?php

class WoguRobotAction extends CommonAction
{
    public function man() {
		$model = M();
		$sql = 'SELECT COUNT(*) AS tcount FROM '.C("DB_PREFIX").'wogu_robot_answers';
		$count = $model->query($sql);
		$count = $count[0]['tcount'];
		
		$sql = 'SELECT * FROM '.C("DB_PREFIX").'wogu_robot_answers WHERE 1';
		$this->_sqlList($model,$sql,$count,array(),'id');
		foreach($this->list as $v) {
		    $rs = D('WoguRobotKeyword')->select(array('where' => array('aid' => $v['id'])));
		    $arr = array();
		    foreach($rs as $v1) {
		        $arr[] = $v1['keyword'];
		    }
		    $keywords[$v['id']] = implode(',', $arr);
		}
		
		$this->assign('keywords', $keywords);
        $this->display();
    }
    
    public function add() {
        if($_GET['op'] == 'del') {
            $id = intval($_GET['id']);
            D('WoguRobotKeyword')->query('DELETE FROM '.C("DB_PREFIX").'wogu_robot_keyword WHERE id='.$id);
            $this->success('删除成功', $_SERVER['HTTP_REFERER']);
        } elseif($_POST['doadd']) {
            $aid = intval($_POST['id']);
            $answer = $_POST['answer'];
            $_POST['suggest1'] = addslashes($_POST['suggest1']);
            $_POST['suggest2'] = addslashes($_POST['suggest2']);
            if($aid) {
                D('WoguRobotAnswers')->query('UPDATE '.C("DB_PREFIX")."wogu_robot_answers
                    SET answer='{$answer}',
                    tip1='{$_POST['tip1']}',
                    suggest1='{$_POST['suggest1']}',
                    tip2='{$_POST['tip2']}',
                    suggest2='{$_POST['suggest2']}'
                    WHERE id={$aid}");
            } else {
                $aid = D('WoguRobotAnswers')->add(array('answer' => $answer, 'tip1' => $_POST['tip1'], 'suggest1' => $_POST['suggest1'], 'tip2' => $_POST['tip2'], 'suggest2' => $_POST['suggest2']));
            }
            
            if($aid && !empty($_POST['keywords'])) {
                $keywords = explode(' ', $_POST['keywords']);
                foreach($keywords as $v) {
                    $r = D('WoguRobotKeyword')->query('SELECT COUNT(*) AS total FROM '.C("DB_PREFIX").'wogu_robot_keyword WHERE keyword=\''.$v.'\' AND aid='.$aid);
                    if($r['total'] == 0)
                        D('WoguRobotKeyword')->add(array('keyword' => $v, 'aid' => $aid));
                }
            }
            
            $this->success('添加成功', $_SERVER['HTTP_REFERER']);
        }

        $id = intval($_GET['id']);
        if($id) {
            $answer = D('WoguRobotAnswers')->query('SELECT * FROM '.C("DB_PREFIX").'wogu_robot_answers WHERE id='.$id);
            $keywords = D('WoguRobotKeyword')->query('SELECT * FROM '.C("DB_PREFIX").'wogu_robot_keyword WHERE aid='.$id);
            $this->assign('aid', $id);
            $this->assign('answer', $answer[0]);
            $this->assign('keywords', $keywords);
        }
        $this->display();
    }
    
    public function unknow() {
		$model = M();
		$sql = 'SELECT COUNT(*) AS tcount FROM '.C("DB_PREFIX").'wogu_robot_unknow';
		$count = $model->query($sql);
		$count = $count[0]['tcount'];
		
		$sql = 'SELECT * FROM '.C("DB_PREFIX").'wogu_robot_unknow WHERE 1';
		$this->_sqlList($model,$sql,$count,array(),'id');
        $this->display();
    }
    
    public function comment() {
		$model = M();
		$sql = 'SELECT COUNT(*) AS tcount FROM '.C("DB_PREFIX").'user WHERE feedback1!=\'\'';
		$count = $model->query($sql);
		$count = $count[0]['tcount'];
		
		$sql = 'SELECT * FROM '.C("DB_PREFIX").'user WHERE feedback1!=\'\'';
		$this->_sqlList($model,$sql,$count,array(),'uid');
        $this->display();
    }
}